import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login/login.vue'
import index from '@/views/index/index.vue'
import search from '@/views/search/search.vue'
import searchList from '@/views/searchList/searchList.vue'
import detail from '@/views/detail/detail.vue'
import pay from '@/views/pay/pay.vue'
import orders from '@/views/orders/orders.vue'
import home from '@/views/index/home.vue'
import classify from '@/views/index/classify.vue'
import cart from '@/views/index/cart.vue'
import user from '@/views/index/user.vue'
import ProductReviews from '@/views/detail/ProductReviews.vue'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes: [
    { path: '/login', component: login },
    {
      path: '/',
      component: index,
      redirect: '/home',
      children: [
        { path: '/home', component: home },
        { path: '/classify', component: classify },
        { path: '/cart', component: cart },
        { path: '/user', component: user }
      ]
    },
    { path: '/search', component: search },
    { path: '/searchList', component: searchList },
    { path: '/detail/:goodsId', component: detail },
    { path: '/ProductReviews', component: ProductReviews },
    { path: '/pay', component: pay },
    { path: '/orders', component: orders }
  ]
})
const Mypath = ['/pay', '/orders', '/cart']
router.beforeEach((to, from, next) => {
  if (!Mypath.includes(to.path)) {
    next()
    return false
  }
  const token = store.state.user.userinfo.token
  if (token) {
    next()
  } else {
    next('/login')
  }
})
export default router
